package com.paranoidvulture.javaEngine.Views;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;

import com.paranoidvulture.javaEngine.MainPackage.ScreenManager;
import com.paranoidvulture.javaEngine.MapLayers.EditorLayer;
import com.paranoidvulture.javaEngine.Utilities.Button;
import com.paranoidvulture.javaEngine.Utilities.ButtonListener;
import com.paranoidvulture.javaEngine.Utilities.Camera2D;
import com.paranoidvulture.javaEngine.Utilities.ControlHandler;
import com.paranoidvulture.javaEngine.Utilities.TextBox;

/* MapEditor View
 * This view is for making maps
 * The init() method is where all variable objects need to get instanced unless
 *  it is a special case.
 * Variables need to be at the top underneath the constructor
 * Any Custom Methods go after the init() method and before the 
 *  update(long timePassed) method
 * All objects that have an update() method within them will go in the update method,
 *  likewise, any object that needs to be drawn, will go in the draw method
 */
public class MapEditor extends ViewBase implements ButtonListener {

		public MapEditor(ScreenManager s){
			super(s);
		}
		
		//variables
		Camera2D cam, gui;
		int gridWidth = 64;
		int gridHeight = 64;
		int numGridHor;
		int numGridVer;
		ControlHandler cHandler;
		TextBox textBox1;
		Button button1, button2;	
		EditorLayer eL;
		int leftEdge;
		
		public void init(){
			this.setEnabled(false);
			cam = new Camera2D(this.s,(int)(s.getWidth()*.75),(int)(s.getHeight()));
			gui = new Camera2D(this.s, (int)(s.getWidth()*.25),(int)(s.getHeight()));
			leftEdge = (int)(s.getWidth() * .75);
			cHandler = new ControlHandler();
			numGridHor = getHorizontalGrid();
			numGridVer = getVerticalGrid();
			textBox1 = new TextBox();
			button1 = new Button();
			textBox1.init();
			button2 =new Button();
			button2.init("Other Button");
			button2.setActive(this.getEnabled());
			button2.setPosition(leftEdge, 300);
			button1.init("Test Button");
			button1.setActive(this.getEnabled());
			textBox1.setActive(this.getEnabled());
			button1.setPosition(leftEdge,400);
			textBox1.setPosition(leftEdge,200);
			textBox1.setText("This is Text");
			cHandler.addToList(textBox1);
			cHandler.addToList(button1);
			cHandler.addToList(button2);
			button1.addBListeners(this);
			button2.addBListeners(this);
			
			eL = new EditorLayer();
			eL.init(20, 20);
		}

		public int getHorizontalGrid(){
			int screenWidth = cam.getCameraWidth();
			numGridHor = screenWidth/gridWidth;
			return numGridHor;			
		}
		
		public int getVerticalGrid(){
			int screenHeight = cam.getCameraHeight();
			 numGridVer = screenHeight/gridHeight;
			return numGridVer;
		}
		
		public void update(long timePassed){
			cHandler.setActiveStateAll(this.getEnabled());
			cHandler.updateControls(timePassed);
			cam.LockCamera(gridWidth, gridWidth, eL.getMapWidth(), eL.getMapHeight());
		}
		
		public void draw(Graphics2D g){
			Rectangle2D rec = new Rectangle2D.Double(0, 0, cam.getCameraWidth(), cam.getCameraHeight());
			Rectangle2D guiRec = new Rectangle2D.Double(leftEdge,0,gui.getCameraWidth(), gui.getCameraHeight());
			g.setColor(Color.orange);
			g.fill(rec);
			eL.draw(g, cam);
			g.setColor(Color.yellow);
			g.fill(guiRec);
			cHandler.draw(g);
		}
		
		@Override
		public void keyPressed(KeyEvent e) {
			cHandler.updateKeyEventPressed(e);
			if (e.getKeyCode() == KeyEvent.VK_UP) {
				cam.ScrollUp();
			}
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				cam.ScrollDown();			
			}
			if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				cam.ScrollLeft();
			}
			if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				cam.ScrollRight();
			}
		}

		@Override
		public void keyReleased(KeyEvent e) {
			cHandler.updateKeyEventReleased(e);
		}

		@Override
		public void keyTyped(KeyEvent e) {
			cHandler.updateKeyEventTyped(e);
		}

		@Override
		public void mouseClicked(MouseEvent e) {
			cHandler.updateMouseClicked(e);
			eL.mouseClicked(e);
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			cHandler.updateMouseEntered(e);
		}

		@Override
		public void mouseExited(MouseEvent e) {
			cHandler.updateMouseExited(e);
		}

		@Override
		public void mousePressed(MouseEvent e) {
			cHandler.updateMousePressed(e);
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			cHandler.updateMouseReleased(e);
		}

		@Override
		public void mouseWheelMoved(MouseWheelEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseDragged(MouseEvent e) {
			cHandler.updateMouseDragged(e);
		}

		@Override
		public void mouseMoved(MouseEvent e) {
			cHandler.updateMouseMoved(e);
			eL.mouseMoved(e);
		}

		@Override
		public void buttonClick(Button e) {
			if(e==button1) {
				if (button1.getName() == "Test Button") {
					button1.setName("Clicked B1");
				} else {
					button1.setName("Test Button");
				}
			} else if (e== button2) {
				if (button2.getName() == "Other Button") {
					button2.setName("Clicked B2");
				} else {
					button2.setName("Other Button");
				}
			}
		}
}
